<template>
  <div class="app-container">
    <tree-table :data="data" :columns="columns" border @selected="mockTableData">
      <el-table-column label="组织结构名称">
        <template slot-scope="scope">
          <span style="color:sandybrown">{{scope.row.name}}</span>
        </template>
      </el-table-column>

      <el-table-column label="排序" width="120">
        <template slot-scope="scope">
          <span style="color:sandybrown">{{scope.row.orders}}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" width="120">
        <template slot-scope="scope">
          <span style="color:sandybrown">{{scope.row.isDel == false ? '是' : '否'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <div style="text-align:center">
            <i class="el-icon-circle-plus" @click="addData(scope.row)"
               style="margin-right:10px;color:black;cursor:pointer"></i>
            <i class="el-icon-edit" @click="editData(scope.row)" v-show="!scope.row.root"
               style="margin-left:10px;margin-right:10px;color:black;cursor:pointer"></i>
            <i class="el-icon-delete" @click="deleteData(scope.row)" v-show="!scope.row.root"
               style="margin-left:10px;color:black;cursor:pointer"></i>
          </div>
        </template>
      </el-table-column>
    </tree-table>


    <el-dialog :title='menuName' :visible.sync="dialogShow" center :before-close="preClose" width="35%">
      <el-tabs type="border-card">
        <el-tab-pane label="组织信息">
          <el-form :model="form" ref="form" :rules="rules">

            <el-form-item label="组织名称" :label-width="formLabelWidth" prop="name">
              <el-input v-model="form.name"></el-input>
            </el-form-item>

            <el-form-item label="排序" :label-width="formLabelWidth" prop="orders">
              <el-input v-model="form.orders"></el-input>
            </el-form-item>

          </el-form>
        </el-tab-pane>
        <el-tab-pane label="角色信息">
          <el-transfer
            v-model="ownRoles"
            :titles="['未分配角色', '已分配角色']"
            :props="{key: 'dbid', label: 'name'}"
            @change="handleChange"
            :data="roles">
          </el-transfer>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormClose">取 消</el-button>
        <el-button type="primary" @click="confirmDialog()">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import treeTable from '@/components/TreeTable'
  import {mapGetters} from 'vuex';
  import {getAllRoles} from 'api/role';
  import {getOwOrgRoleList} from 'api/org';

  export default {
    name: 'menuTree',
    components: {
      treeTable
    },
    data() {
      return {
        data: [],
        menuName: '',
        ownRoles: [],  //左 已分配角色
        roles: [{dbid: 1, name: '111', disabled: false}], //右 未分配角色
        addRoles: [],
        delRoles: [],
        formLabelWidth: '80px',
        dialogShow: false,
        form: {},
        node: {},
        rules: {
          name: [
            {required: true, message: '请输入组织名称', trigger: 'blur'}
          ],
          orders: [
            {required: true, message: '请输入排序', trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      columns() {
        let cols = [];
        cols.push({
          text: '组织名称',
          align: 'center',
          value: 'dataName',
          width: 300
        });
        return cols;
      }
    },
    methods: {

      addData(node) {
        this.getOrgRoleList(0);
        this.node = node;
        this.form = {};
        this.form.parentDbid = node.dbid;
        this.form.isDel = false;
        this.form.start = 0;
        this.form.limit = 999999;

        this.dialogShow = true;


      },
      editData(node) {

        this.getOrgRoleList(node.dbid);
        this.node = node.parent;
        this.form = {};
        this.form.parentDbid = node.dbid;
        this.$store.dispatch("loadData", node.dbid).then(res => {
          this.form = this.$store.state.org.orgEdit
        })
        this.dialogShow = true;

      },
      deleteData(node) {
        this.node = node.parent;
        this.$confirm("确定要删除选中的数据?").then(res => {
          let id = node.dbid;
          this.$store.dispatch('deleteOrg', id).then(res => {
            this.$message("操作成功")
            this.reloadData();
          })
        })


      },
      getOrgRoleList(dbid) {
        this.roles = [];
        this.ownRoles = [];
        this.addRoles = [];
        this.delRoles = [];
        getAllRoles().then(info => {
          this.roles = info.data
        });
        getOwOrgRoleList(dbid).then(info => {
          info.data.forEach(d => {
            this.ownRoles.push(d.dbid);
          });
        });
      },
      preClose() {
        this.$confirm('确认关闭？')
          .then(_ => {
            this.dialogShow = false;
          }).catch(_ => {
        });
      },

      message(data) {

      },
      closeDialog() {
        this.$confirm('确认关闭此窗口？')
          .then(_ => {
            this.dialogShow = false;
          }).catch(_ => {
        });
      },
      handleChange(direction, movedKeys, value) {
        if (movedKeys === 'right') {
          value.forEach(dbid => {
            let idx = this.delRoles.indexOf(dbid);
            if (idx !== -1) {
              this.delRoles.splice(idx, 1);
            } else {
              this.addRoles.push(dbid)
            }
          })
        }
        if (movedKeys === 'left') {
          value.forEach(dbid => {
            let idx = this.addRoles.indexOf(dbid);
            if (idx !== -1) {
              this.addRoles.splice(idx, 1);
            } else {
              this.delRoles.push(dbid)
            }
          })
        }
      },

      confirmDialog() {

        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            this.form.addRoles = this.addRoles.toString();
            this.form.delRoles = this.delRoles.toString();
            this.$store.dispatch("SaveOrg", this.form).then(res => {
              this.dialogShow = false;
              this.reloadData();
            })
          } else {
            this.$message({
              type: 'error',
              message: '请正确填写表单数据!'
            });
            return false;
          }
        });


      },
      dialogFormClose() {
        this.form = {name: '', orders: ''},
          this.dialogShow = false;
      },
      mockTableData(node) {
        let id;
        if (node == null) {
          id = '';
        } else {
          id = node.dbid;
        }

        if (id == '') {
          this.$store.dispatch('orgList', id).then(() => {
            this.data = this.$store.state.org.orgList;
          })
        } else {
          this.$store.dispatch('orgList', id).then(() => {
            let d = this.$store.state.org.orgList;
            this.$set(node, 'children', d)
          })

        }
      },
      reloadData() {
        if (this.node.children) {
          this.node.children.splice(0, this.node.children);
        }
        this.mockTableData(this.node);
      }

    },

    created() {
      this.mockTableData(null)

    }
  }
</script>
<style>
  .el-table th > .cell {
    position: relative;
    word-wrap: normal;
    text-overflow: ellipsis;
    vertical-align: middle;
    width: 100%;
    box-sizing: border-box;
    text-align: center !important;
  }
</style>
